package com.winning.xffw.service.service.impl;

import com.winning.xffw.service.service.ITestService;
import com.winning.xffw.service.jdbc.JdbcTemplateRepository;
import lombok.extern.slf4j.Slf4j;
import org.postgresql.copy.CopyManager;
import org.postgresql.core.BaseConnection;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.FileInputStream;
import java.sql.Connection;

/**
 * @Author: shipengfei
 * @Description:
 * @Date: 2019-05-28 9:13
 */
@Service
@Slf4j
public class TestServiceImpl implements ITestService {

    @Resource
    private JdbcTemplateRepository jdbcTemplateRepository;

    @Override
    public void copyCsvDataTest() {
        String filePath = "F:\\2.csv";
        String sql = "COPY gp_table_test_1(subject,sex,column_h,column_g,column_f,column_e,id_a,column_d,column_c,column_b,id_b,column_a,name,card) FROM STDIN WITH csv header DELIMITER AS ',' LOG ERRORS SEGMENT REJECT LIMIT 1000000;";
        try (FileInputStream fileInputStream = new FileInputStream(filePath);
             Connection conn = jdbcTemplateRepository.getJdbcTemplate().getDataSource().getConnection()) {
            CopyManager copyManager = new CopyManager((BaseConnection) conn.getMetaData().getConnection());
            Long startTime = System.currentTimeMillis();
            Long count = copyManager.copyIn(sql, fileInputStream);
            Long endTime = System.currentTimeMillis();
            log.info("-------->>" + count + "|" + (endTime - startTime));
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}
